﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using CMDBImportCIDLL;

namespace CMDBImportCITool
{
    public partial class Main : Form
    {
        private string path;
        private string server;
        private string login;
        private string password;
        private Company company;
        public Main()
        {
            InitializeComponent();
            this.radioButton1.Checked = true;
            this.textBox2.Text = "192.168.85.181";
            this.textBox3.Text = "Demo";
            this.textBox4.Text = "asb#1234";

            this.comboBox1.DataSource = ALLCompany.CompanyList;
            this.comboBox1.DisplayMember = "CompanyName";
            this.comboBox1.ValueMember = "CompanyPermission";


        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog file = new OpenFileDialog();
            if (file.ShowDialog() == DialogResult.OK)
            {
                if (Path.GetExtension(file.FileName).ToLower() != ".xls")
                {
                    MessageBox.Show("Warning!" + Environment.NewLine + "请选择 Microsoft Excel 97-2003 工作表 (.xls)");
                    return;
                }
                this.textBox1.Text = file.FileName;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            path = this.textBox1.Text;
            server = this.textBox2.Text;
            login = this.textBox3.Text;
            password = this.textBox4.Text;
            Company select = new Company(this.comboBox1.Text,this.comboBox1.SelectedValue.ToString());
            company = select;
            if (!string.IsNullOrEmpty(path) && !string.IsNullOrEmpty(server) && !string.IsNullOrEmpty(login) && !string.IsNullOrEmpty(password))
            {
                Thread thread = new Thread(new ThreadStart(ImportData));
                thread.Start();
            }

        }

        private void ImportData()
        {

            if (!File.Exists(path))
            {
                MessageBox.Show("Warning!" + Environment.NewLine + path + "不存在！");
                return;
            }
            AllUnEnabled();
            MainProgress mainp = new MainProgress();
            this.richTextBox1.Invoke((MethodInvoker)delegate()
            {
                this.richTextBox1.Text = string.Empty;
            });
            try
            {
                if (this.radioButton1.Checked)
                {
                    mainp.Import(server, login, password, company, path, new ShowMessageDelegate(ShowLog), ImportOperator.CreateSkipUpdate);
                }
                else
                {
                    mainp.Import(server, login, password, company, path, new ShowMessageDelegate(ShowLog), ImportOperator.CreateAndUpdate);
                }
                MessageBox.Show("导入成功！");
                ShowLog("导入成功！");
            }
            catch (Exception e)
            {
                MessageBox.Show("Error!" + Environment.NewLine + "出错了,请检查日志信息");
                ShowLog(e.StackTrace + Environment.NewLine + e.Message);
            }
            AllEnabled();
            
        }

        private void AllEnabled()
        {
            this.Invoke((MethodInvoker)delegate()
            {
                this.textBox1.Enabled = true;
                this.textBox2.Enabled = true;
                this.textBox3.Enabled = true;
                this.textBox4.Enabled = true;
                this.button1.Enabled = true;
                this.button2.Enabled = true;
                this.button3.Enabled = true;
                this.radioButton1.Enabled = true;
                this.radioButton2.Enabled = true;
                this.comboBox1.Enabled = true;
            });
        }

        private void AllUnEnabled()
        {
            this.Invoke((MethodInvoker)delegate()
            {
                this.textBox1.Enabled = false;
                this.textBox2.Enabled = false;
                this.textBox3.Enabled = false;
                this.textBox4.Enabled = false;
                this.button1.Enabled = false;
                this.button2.Enabled = false;
                this.button3.Enabled = false;
                this.radioButton1.Enabled = false;
                this.radioButton2.Enabled = false;
                this.comboBox1.Enabled = false;
            });
        }

        //private void ShowProgressBar()
        //{
        //    int i = 0;
        //    while (i < 100)
        //    {
        //        this.Invoke((MethodInvoker)delegate()
        //        {
        //            this.progressBar1.Value = i;
        //        });

        //        i++;
        //        Thread.Sleep(100);
        //    }
        //}



        public void ShowLog(string message)
        {
            this.richTextBox1.Invoke((MethodInvoker)delegate()
            {
                this.richTextBox1.Text = this.richTextBox1.Text + Environment.NewLine + message;
            });
        }

        private void button3_Click(object sender, EventArgs e)
        {
            path = this.textBox1.Text;
            server = this.textBox2.Text;
            login = this.textBox3.Text;
            password = this.textBox4.Text;
            Company select = new Company(this.comboBox1.Text, this.comboBox1.SelectedValue.ToString());
            company = select;
            if (!string.IsNullOrEmpty(path) && !string.IsNullOrEmpty(server) && !string.IsNullOrEmpty(login) && !string.IsNullOrEmpty(password))
            {
                Thread thread = new Thread(new ThreadStart(DeleteData));
                thread.Start();
            }
        }

        private void DeleteData()
        {

            if (!File.Exists(path))
            {
                MessageBox.Show("Warning!" + Environment.NewLine + path + "不存在！");
                return;
            }
            AllUnEnabled();
            MainProgress mainp = new MainProgress();
            this.richTextBox1.Invoke((MethodInvoker)delegate()
            {
                this.richTextBox1.Text = string.Empty;
            });
            try
            {
                mainp.Import(server, login, password, company, path, new ShowMessageDelegate(ShowLog), ImportOperator.Delete);
                MessageBox.Show("删除成功！");
                ShowLog("删除成功！");
            }
            catch (Exception e)
            {
                MessageBox.Show("Error!" + Environment.NewLine + "出错了,请检查日志信息");
                ShowLog(e.StackTrace + Environment.NewLine + e.Message);
            }
            AllEnabled();
           
        }
    }
}
